package com.cinemtl.manager;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.cinemtl.beans.Movie;
import com.cinemtl.dbConnection.DbConnection;

public class MovieManager {
	public static ArrayList<Movie> getAll() {
		ArrayList<Movie> movies = new ArrayList<Movie>();
		String query = "SELECT * FROM movie";

		PreparedStatement ps;

		try {
			ps = DbConnection.getConnection().prepareStatement(query);

			ResultSet result = ps.executeQuery();

			while (result.next()) {
				Movie movie = new Movie();
				movie.setId(result.getInt("id"));
				movie.setTitle(result.getString("title"));
				movie.setType(result.getString("type"));
				movie.setDuration(result.getInt("duration"));
				movie.setUrlImage(result.getString("url_image"));
				movie.setUrlVideo(result.getString("url_video"));
				movie.setDescription(result.getString("description"));
				movies.add(movie);
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		DbConnection.closeConnection();

		return movies;
	}

	public static Movie getById(int idMovie) {
		Movie movie = new Movie();
		String query = "SELECT * FROM movie WHERE id = ?";

		PreparedStatement ps;

		try {
			ps = DbConnection.getConnection().prepareStatement(query);
			ps.setInt(1, idMovie);
			ResultSet result = ps.executeQuery();

			while (result.next()) {
				movie.setId(result.getInt("id"));
				movie.setTitle(result.getString("title"));
				movie.setType(result.getString("type"));
				movie.setDuration(result.getInt("duration"));
				movie.setUrlImage(result.getString("url_image"));
				movie.setUrlVideo(result.getString("url_video"));
				movie.setDescription(result.getString("description"));
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		DbConnection.closeConnection();

		return movie;
	}

	public static ArrayList<Movie> getByType(String type) {
		ArrayList<Movie> movies = new ArrayList<Movie>();
		Movie movie = new Movie();
		String query = "SELECT * FROM movie WHERE type = ?";

		PreparedStatement ps;

		try {
			ps = DbConnection.getConnection().prepareStatement(query);
			ps.setString(1, type);
			ResultSet result = ps.executeQuery();

			while (result.next()) {
				movie.setId(result.getInt("id"));
				movie.setTitle(result.getString("title"));
				movie.setType(result.getString("type"));
				movie.setDuration(result.getInt("duration"));
				movie.setUrlImage(result.getString("url_image"));
				movie.setUrlVideo(result.getString("url_video"));
				movie.setDescription(result.getString("description"));
				movies.add(movie);
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		DbConnection.closeConnection();

		return movies;
	}

	public static Movie getByTitle(String title) {
		Movie movie = new Movie();
		String query = "SELECT * FROM movie WHERE title = ?";

		PreparedStatement ps;

		try {
			ps = DbConnection.getConnection().prepareStatement(query);
			ps.setString(1, title);
			ResultSet result = ps.executeQuery();

			while (result.next()) {
				movie.setId(result.getInt("id"));
				movie.setTitle(result.getString("title"));
				movie.setType(result.getString("type"));
				movie.setDuration(result.getInt("duration"));
				movie.setUrlImage(result.getString("url_image"));
				movie.setUrlVideo(result.getString("url_video"));
				movie.setDescription(result.getString("description"));
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		DbConnection.closeConnection();

		return movie;
	}
	
	public static void insertMovie(Movie movie)
	{
		String query = "INSERT INTO movie (title,type,duration,url_image,url_video,description) "
				+ "value(?,?,?,?,?,?)";

		PreparedStatement ps;

		try {
			ps = DbConnection.getConnection().prepareStatement(query);
			ps.setString(1, movie.getTitle());
			ps.setString(2, movie.getType());
			ps.setInt(3, movie.getDuration());
			ps.setString(4, movie.getUrlImage());
			ps.setString(5, movie.getUrlVideo());
			ps.setString(6, movie.getDescription());

			ps.executeUpdate();

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		DbConnection.closeConnection();
	}
	
	public static void updateMovie(Movie movie)
	{
		String query = "UPDATE movie SET title=? ,type=?,duration=?,url_image=?,url_video=?,description=? WHERE id=?";
				

		PreparedStatement ps;

		try {
			ps = DbConnection.getConnection().prepareStatement(query);
			ps.setString(1, movie.getTitle());
			ps.setString(2, movie.getType());
			ps.setInt(3, movie.getDuration());
			ps.setString(4, movie.getUrlImage());
			ps.setString(5, movie.getUrlVideo());
			ps.setString(6, movie.getDescription());
			ps.setInt(7, movie.getId());
			
			ps.executeUpdate();

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		DbConnection.closeConnection();
	}
	
	public static void deleteMovie(int idMovie)
	{
		String query = "DELETE FROM movie WHERE id=?  ";
		PreparedStatement ps;
		
		try {
			ps = DbConnection.getConnection().prepareStatement(query);
			ps.setInt(1, idMovie);

			ps.executeUpdate();

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		DbConnection.closeConnection();
	}	
	
	
	public static ArrayList<Movie> searchMovie(String title) {
		ArrayList<Movie> movies = new ArrayList<Movie>();
		
		String query = "SELECT * FROM movie WHERE title like ?";
		PreparedStatement ps;

		try {
			ps = DbConnection.getConnection().prepareStatement(query);
			ps.setString(1, "%"+title+"%");
			ResultSet result = ps.executeQuery();

			while (result.next()) {
				Movie movie = new Movie();
				movie.setId(result.getInt("id"));
				movie.setTitle(result.getString("title"));
				movie.setType(result.getString("type"));
				movie.setDuration(result.getInt("duration"));
				movie.setUrlImage(result.getString("url_image"));
				movie.setUrlVideo(result.getString("url_video"));
				movie.setDescription(result.getString("description"));
				
				movies.add(movie);
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		DbConnection.closeConnection();

		return movies;
	}
}
